//
//  ST_NavView.m
//  ST_BaseMall
//
//  Created by 昱胜 on 2022/9/2.
//

#import "ST_NavView.h"

@interface ST_NavView ()

/* bottomLine */
@property(nonatomic, strong) UIView *bottomLine;

@end

@implementation ST_NavView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configUI];
        self.backgroundColor = [UIColor appWhiteLabelColor];
    }
    return self;
}

- (void)setShowBottomLine:(BOOL)showBottomLine {
    self.bottomLine.hidden = !showBottomLine;
}

- (void)configUI {
    
    [self addSubview:self.leftButton];
    WeakSelf(weakSelf);
    [[self.leftButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.leftBlock) {
            weakSelf.leftBlock();
        } else {
            if ([UIViewControllerHelper findCurrentShowingViewController].navigationController.viewControllers.count > 1) {
                [[UIViewControllerHelper findCurrentShowingViewController].navigationController popViewControllerAnimated:YES];
            } else if ([UIViewControllerHelper findCurrentShowingViewController].presentingViewController) {
                [[UIViewControllerHelper findCurrentShowingViewController] dismissViewControllerAnimated:YES completion:nil];
                [[UIViewControllerHelper findCurrentShowingViewController].view endEditing:YES];
            }
        }
    }];
    
    [self addSubview:self.titleLabel];
    
    [self addSubview:self.rightButton];
    [[self.rightButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.rightBlock) {
            weakSelf.rightBlock();
        }
    }];
    
    self.bottomLine = [[UIView alloc]initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight-1, kScreenWidth, 1)];
    self.bottomLine.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    self.bottomLine.hidden = YES;
    [self addSubview:self.bottomLine];
    
}

- (UIButton *)leftButton {
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftButton setFrame:CGRectMake(7, kStatusBarHeight, 33, 44)];
        [_leftButton setImage:[UIImage imageNamed:@"base_nav_back_black"] forState:UIControlStateNormal];
    }
    return _leftButton;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, kStatusBarHeight, kScreenWidth-80, 44)];
        _titleLabel.text = @"";
        _titleLabel.textColor = [UIColor appBlackLabelColor];
        _titleLabel.font = [UIFont ys_boldFontWithSize:18];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UIButton *)rightButton {
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightButton setFrame:CGRectMake(kScreenWidth-54, kStatusBarHeight, 44, 44)];
        _rightButton.hidden = YES;
    }
    return _rightButton;
}

@end
